/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.subversion.server.model;

import java.util.Set;
import java.util.Map;
import java.util.Date;
import org.internna.iwebmvc.utils.Assert;
import org.internna.iwebjtracker.server.scm.model.LogEntry;
import org.internna.iwebjtracker.server.scm.model.ChangeSet;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.SVNLogEntryPath;

/**
 * Wrapper for log entries.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public final class SVNKitLogEntry implements LogEntry<SVNKitRevision, SVNKitChange> {

    private final SVNLogEntry logEntry;
    private final ChangeSet<SVNKitChange> changeSet;

    /**
     * Initialize the wrapper (in particular the changeset object).
     *
     * @param logEntry a non null instance
     */
    @SuppressWarnings("unchecked")
    public SVNKitLogEntry(SVNLogEntry logEntry) {
        Assert.notNull(logEntry);
        this.logEntry = logEntry;
        this.changeSet = new ChangeSet<SVNKitChange>();
        Set<Map.Entry<String, SVNLogEntryPath>> entries = logEntry.getChangedPaths().entrySet();
        for (Map.Entry<String, SVNLogEntryPath> entry : entries) {
            changeSet.addChange(new SVNKitChange(entry.getValue()));
        }
    }

    /**
     * Delegates to the wrapped object.
     *
     * @return any
     */
    @Override public String getAuthor() {
        return logEntry.getAuthor();
    }

    /**
     * Delegates to the wrapped object.
     *
     * @return any
     */
    @Override public Date getDate() {
        return logEntry.getDate();
    }

    /**
     * Wraps the revision number in a new instance.
     *
     * @return a non null object
     */
    @Override public SVNKitRevision getRevision() {
        return new SVNKitRevision(SVNRevision.create(logEntry.getRevision()));
    }

    /**
     * Delegates to the wrapped object.
     *
     * @return any
     */
    @Override public String message() {
        return logEntry.getMessage();
    }

    /**
     * Accessor method.
     *
     * @return a non null instance.
     */
    @Override public ChangeSet<SVNKitChange> getChangeSet() {
        return changeSet;
    }

    /**
     * Informs of the revision and number of changes of this entry.
     *
     * @return a non null non empty string
     */
    @Override public String toString() {
        return "Revision: " + logEntry.getRevision() + "; Changeset: " + getChangeSet().getChanges().size();
    }

}
